Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose NodeData enum #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Expose NodeData enum #28

wants to merge 1 commit into from

Conversation

xamgore
Copy link

@xamgore xamgore commented Jun 7, 2023

Imagine you would like to iterate through Text nodes. It may be helpful when you have non-structured text intermixed with markup nodes.

<div class="cls">
  <strong>Title</strong>
  1 | 2 | 3
  Love you
  <span>18+</span>
  John Doe
</div>
node.select(".cls").nodes().iter().filter(|it| it.query(|n| match n.data {
    NodeData::Text{ .. } => true,
    _ => false,
})),

The other way around would look like this:

node
  .select(".cls")
  .nodes()
  .iter()
  .flat_map(|node| node.children())
  .filter(|node| node.is_text())
  .map(|node| node.text())
  .collect::<Vec<_>>();

But this approach won't work for HTML comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant